package cn.itcast.avro;

import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.specific.SpecificDatumReader;
import org.apache.avro.specific.SpecificDatumWriter;

import java.io.File;
import java.io.IOException;

public class UserTest {
    public static void main(String[] args) throws IOException {
        User user = new User("liuqi", 22, "平凉");
        User user1 = User.newBuilder().setAddress("庄浪").setAge(23).setName("琪琪").build();

        //序列化
        //SpecificDatumWriter<User> userSpecificDatumWriter = new SpecificDatumWriter<>(User.class);
        //DataFileWriter<User> userDataFileWriter = new DataFileWriter<>(userSpecificDatumWriter);
        //userDataFileWriter.create(user.getSchema(),new File("avro.txt"));
        //userDataFileWriter.append(user);
        //userDataFileWriter.append(user1);
        //userDataFileWriter.close();


        //反序列化
        SpecificDatumReader<User> userSpecificDatumReader = new SpecificDatumReader<>();
        DataFileReader<User> users = new DataFileReader<>(new File("avro.txt"), userSpecificDatumReader);
        for (User user2 : users) {
            System.out.println(user2);
        }

    }
}
